package com.sos.widget.binding;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.kunminx.binding_recyclerview.layout_manager.WrapContentGridLayoutManager;
import com.kunminx.binding_recyclerview.layout_manager.WrapContentLinearLayoutManager;
import com.kunminx.binding_recyclerview.layout_manager.WrapContentStaggeredGridLayoutManager;
import com.sos.widget.view.recyclerview.LineManagers;
import com.sos.widget.view.recyclerview.layout_manager.AutoWrapContentLinearLayoutManager;

import java.util.List;

public class RecyclerViewBindingAdapter {

    @BindingAdapter(value = {"saas_recycler_adapter",
            "saas_recycler_submitList",
            "saas_recycler_autoScrollToTopWhenInsert",
            "saas_recycler_autoScrollToBottomWhenInsert",
            "saas_recycler_lineManager",
            "saas_recycler_level",
            "saas_recycler_auto",
            "saas_recycler_flickerCancel",
            "saas_recycler_spanCount"}, requireAll = false)
    public static void bindList(RecyclerView recyclerView,
                                ListAdapter adapter,
                                List list,
                                boolean autoScrollToTopWhenInsert,
                                boolean autoScrollToBottomWhenInsert,
                                LineManagers.LineManagerFactory driver,
                                boolean isLevel,
                                boolean auto,
                                boolean flickCancel,
                                int mSpanCount) {

        if (recyclerView != null && list != null && adapter != null) {
            if (flickCancel) {
                /*处理闪烁问题*/
                if (recyclerView.getItemAnimator() != null) {
                    recyclerView.getItemAnimator().setAddDuration(0);
                    recyclerView.getItemAnimator().setChangeDuration(0);
                    recyclerView.getItemAnimator().setMoveDuration(0);
                    recyclerView.getItemAnimator().setRemoveDuration(0);
                    recyclerView.setItemAnimator(null);
                }

            }
            if (recyclerView.getAdapter() == null) {
                if (recyclerView.getLayoutManager() != null) {
                    if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                        int spanCount = ((GridLayoutManager) recyclerView.getLayoutManager()).getSpanCount();
                        recyclerView.setLayoutManager(new WrapContentGridLayoutManager(recyclerView.getContext(), mSpanCount == 0 ? spanCount : mSpanCount));

                    } else if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
                        if (auto) {
                            AutoWrapContentLinearLayoutManager autoWrapContentLinearLayoutManager = new AutoWrapContentLinearLayoutManager(recyclerView.getContext(), true);
                            recyclerView.setLayoutManager(autoWrapContentLinearLayoutManager);
                        } else {
                            WrapContentLinearLayoutManager wrapContentLinearLayoutManager = new WrapContentLinearLayoutManager(recyclerView.getContext());
                            if (isLevel) {
                                wrapContentLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                            }
                            recyclerView.setLayoutManager(wrapContentLinearLayoutManager);
                        }


                    } else if (recyclerView.getLayoutManager() instanceof StaggeredGridLayoutManager) {
                        int spanCount = ((StaggeredGridLayoutManager) recyclerView.getLayoutManager()).getSpanCount();
                        int orientation = ((StaggeredGridLayoutManager) recyclerView.getLayoutManager()).getOrientation();
                        recyclerView.setLayoutManager(new WrapContentStaggeredGridLayoutManager(spanCount, orientation));
                    }
                }
                if (flickCancel) {
                    adapter.setHasStableIds(true);
                }
                recyclerView.setAdapter(adapter);

                adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
                    @Override
                    public void onItemRangeInserted(int positionStart, int itemCount) {
                        if (autoScrollToTopWhenInsert) {
                            recyclerView.scrollToPosition(0);
                        } else if (autoScrollToBottomWhenInsert) {
                            boolean scroll = true;
                            if (scroll) {
                                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                                if (layoutManager != null) {
                                    layoutManager.setStackFromEnd(true);
                                    layoutManager.scrollToPositionWithOffset(recyclerView.getAdapter().getItemCount() - 1, Integer.MIN_VALUE);
                                }
                            }
                        }
                    }

                    @Override
                    public void onItemRangeRemoved(int positionStart, int itemCount) {
                        super.onItemRangeRemoved(positionStart, itemCount);
                    }
                });
            }
            if (driver != null) {
                recyclerView.addItemDecoration(driver.create(recyclerView));
            }
            adapter.submitList(list);

        }
    }


    @BindingAdapter(value = {"notifyCurrentListChanged"})
    public static void notifyListChanged(RecyclerView recyclerView, boolean notify) {
        if (notify && recyclerView != null && recyclerView.getAdapter() != null) {
            recyclerView.getAdapter().notifyDataSetChanged();
        }
    }


}
